package calculator.view;/*
 * @author     ：zhangbo
 * @date       ：Created in 2021/5/21 21:00
 * @description：计算器的窗体设计
 * @modified By：zhangbo
 * @version:     $
 */

import calculator.controller.*;
import calculator.model.Computer;

import javax.swing.*;
import java.awt.*;

public class CalculatorWindows extends JFrame {
    public Computer computer;
    JButton[] numberButton;
    JButton[] operatorButton;
    JButton dot, porN, back, equation, clear;
    JButton sqrt, reciprocal;
    public JTextField resultShow;
    public JTextField showTempResult;
    public JLabel showOperator;
    public JTextField showDataItem;
    public JTextArea saveComputerProcess;
    public JButton saveButton;
    public JButton copyButton;
    public JButton clearTextButton;

    public CalculatorWindows() {
        computer = new Computer();
        initView();
        initActionListener();
    }

    public void initView() {
        setTitle("计算器 by 张波");
        JPanel panelLeft, panelRight;
        resultShow = new JTextField(10);
        resultShow.setText("");
        resultShow.setHorizontalAlignment(JTextField.LEFT);
        resultShow.setForeground(Color.blue);
        resultShow.setFont(new Font("TimesRoman", Font.BOLD, 14));
        resultShow.setEditable(false);
        resultShow.setBackground(Color.green);
        showTempResult = new JTextField();
        showTempResult.setText("");
        showTempResult.setHorizontalAlignment(JTextField.RIGHT);
        showTempResult.setFont(new Font("Arial", Font.BOLD, 14));
        showTempResult.setBackground(Color.cyan);
        showTempResult.setEditable(false);
        showOperator = new JLabel();
        showOperator.setText(" " + this.computer.getOperator() + "");
        showOperator.setBackground(Color.pink);
        showOperator.setFont(new Font("Arial", Font.BOLD, 18));
        showOperator.setHorizontalAlignment(JTextField.CENTER);
        showDataItem = new JTextField();
        showDataItem.setText("");
        showDataItem.setBackground(Color.white);
        showDataItem.setHorizontalAlignment(JTextField.LEFT);
        showDataItem.setFont(new Font("Arial", Font.BOLD, 14));
        showDataItem.setEditable(false);
        saveComputerProcess = new JTextArea();
        saveComputerProcess.setEditable(false);
        saveComputerProcess.setFont(new Font("宋体", Font.PLAIN, 14));
        numberButton = new JButton[10];
        for (int i = 0; i <= 9; i++) {
            numberButton[i] = new JButton("" + i);
            numberButton[i].setFont(new Font("Arial", Font.BOLD, 20));
        }
        operatorButton = new JButton[4];
        String[] sign = {
                "+", "-", "*", "/"
        };
        for (int i = 0; i < 4; i++) {
            operatorButton[i] = new JButton(sign[i]);
            operatorButton[i].setFont(new Font("Arial", Font.BOLD, 20));
        }
        dot = new JButton(".");
        porN = new JButton("+/-");
        back = new JButton("退格");
        equation = new JButton("=");
        clear = new JButton("c");
        sqrt = new JButton("sqrt");
        reciprocal = new JButton("1/x");
        saveButton = new JButton("保存");
        copyButton = new JButton("复制");
        clearTextButton = new JButton("清除");
        panelLeft = new JPanel();
        panelRight = new JPanel();
        panelLeft.setLayout(new BorderLayout());
        JPanel centerInLeft = new JPanel();
        Box boxH = Box.createHorizontalBox();
        boxH.add(showTempResult);
        boxH.add(showOperator);
        boxH.add(showDataItem);
        panelLeft.add(boxH, BorderLayout.NORTH);
        panelLeft.add(centerInLeft, BorderLayout.CENTER);
        centerInLeft.setLayout(new GridLayout(5, 5));
        centerInLeft.add(numberButton[1]);
        centerInLeft.add(numberButton[2]);
        centerInLeft.add(numberButton[3]);
        centerInLeft.add(operatorButton[0]);
        centerInLeft.add(back);
        centerInLeft.add(numberButton[4]);
        centerInLeft.add(numberButton[5]);
        centerInLeft.add(numberButton[6]);
        centerInLeft.add(operatorButton[1]);
        centerInLeft.add(clear);
        centerInLeft.add(numberButton[7]);
        centerInLeft.add(numberButton[8]);
        centerInLeft.add(numberButton[9]);
        centerInLeft.add(operatorButton[2]);
        centerInLeft.add(reciprocal);
        centerInLeft.add(numberButton[0]);
        centerInLeft.add(porN);
        centerInLeft.add(dot);
        centerInLeft.add(operatorButton[3]);
        centerInLeft.add(sqrt);
        centerInLeft.add(new JLabel());
        centerInLeft.add(new JLabel());
        centerInLeft.add(new JLabel());
        centerInLeft.add(new JLabel());
        centerInLeft.add(equation);
        panelRight.setLayout(new BorderLayout());
        panelRight.add(resultShow, BorderLayout.NORTH);
        panelRight.add(new JScrollPane(saveComputerProcess), BorderLayout.CENTER);
        JPanel southInpanelRight = new JPanel();
        southInpanelRight.add(saveButton);
        southInpanelRight.add(copyButton);
        southInpanelRight.add(clearTextButton);
        panelRight.add(southInpanelRight, BorderLayout.SOUTH);
        JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, panelLeft, panelRight);
        add(split, BorderLayout.CENTER);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
        setBounds(100, 50, 888, 258);
        validate();

    }

    public void initActionListener() {
        HandleDigit handleDigit = new HandleDigit(this);
        for (int i = 0; i <= 9; i++) {
            numberButton[i].addActionListener(handleDigit);
        }
        dot.addActionListener(handleDigit);
        HandleOperator handleOperator = new HandleOperator(this);
        for (int i = 0; i < 4; i++) {
            operatorButton[i].addActionListener(handleOperator);
        }
        porN.addActionListener(new HandlePN(this));
        sqrt.addActionListener(new HandleSqrt(this));
        reciprocal.addActionListener(new HandleReciprocal(this));
        back.addActionListener(new HandleBack(this));
        equation.addActionListener(new HandleEquation(this));
        clear.addActionListener(new HandleClear(this));
        HandleFile handleFile = new HandleFile(this);
        saveButton.addActionListener(handleFile);
        copyButton.addActionListener(handleFile);
        clearTextButton.addActionListener(handleFile);
    }

}